const GAME_OBJECTS = [];

export class GameObject {
    constructor() {
        GAME_OBJECTS.push(this);
        this.timedelta = 0;
        this.has_called_started = false;


    }
    start() {

    }
    update() {

    }
    on_destroy() {

    }
    destroy() {
        this.on_destroy();

        for (let i in GAME_OBJECTS) { //in 遍历的是下标，of 遍历的是值
            const obj = GAME_OBJECTS[i];
            if (obj === this) {
                GAME_OBJECTS.splice(i);
                break;
            }
        }
    }
}
let last_timeStamp; // 上一次执行的时刻
const step = timestamp => { // 每次requestAnimationFrame调用回调函数step的时候
    //会给回调函数传入调用回调函数时的时间作为参数
    for (let obj of GAME_OBJECTS) {
        if (!obj.has_called_started) {
            obj.start();
            obj.has_called_started = true;
        }
        else {
            obj.timedelta = timestamp - last_timeStamp;
            obj.update();
        }
    }
    last_timeStamp = timestamp;
    requestAnimationFrame(step)
}
requestAnimationFrame(step);